package com.itheima.health.service.impl;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

/**
 * @Author: liangWeiQUQ
 * @Description: 动态菜单权限功能实现
 * @DateTime: 2021/3/11 9:20
 **/
@DubboService
@Slf4j
public class MenuServiceImpl  implements MenuService {
    @Autowired
    private MenuDao MenuDao;
    @Autowired
    private UserServiceImpl userService;

    /**
     * @Author: 传闻中的梁大侠
     * @Date: 9:24 2021/3/11
     * @Parms [username]
     * @ReturnType: java.util.List<com.itheima.health.pojo.Menu>
     * @Description: 菜单动态权限显示-->根据username查询权限
     */

    @Override
    public List<Menu> getMenuList(String username) {
        //根据用户名获取user对象
        User user = userService.findByUsername(username);
        //创建菜单权限集合
        List<Menu> menuList = null;
        Set<Role> roles = user.getRoles();
        for (Role role : roles) {
            menuList = MenuDao.getMenuList(role.getId());
            for (Menu menu : menuList) {
                //获取二级菜单
                List<Menu> children = MenuDao.getMenuLists(role.getId(), menu.getId());
                menu.setChildren(children);
            }
        }
        return menuList;
    }
}
